package com.celestial.Service.Impl;

import com.celestial.Domain.DTO.StartAndEndNodes;
import com.celestial.Domain.DTO.PackageLabel;
import com.celestial.Domain.VO.Triple;
import com.celestial.Repository.*;
import com.celestial.Service.Neo4jService;
import com.celestial.Utils.GetSubGraphUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class Neo4jServiceImpl implements Neo4jService {

    @Autowired
    private LabelsRepository labelsRepository;

    @Autowired
    private ConferenceRepository conferenceRepository;

    @Autowired
    private PersonRepository personRepository;

    @Autowired
    private InstituteRepository instituteRepository;

    @Autowired
    private PolicyRepository policyRepository;

    @Autowired
    private DocumentRepository documentRepository;

    @Autowired
    private MeasurementRepository measurementRepository;

    @Autowired
    private RegionRepository regionRepository;

    public List<Triple> getTriple(StartAndEndNodes startAndEndNodes){
        List<Triple> triple = new ArrayList<>();
        List<String> conferenceEndNode = startAndEndNodes.getEndNode();
        List<String> relation = startAndEndNodes.getRelation();
        for(int i = 0; i < conferenceEndNode.size(); i++){
            Triple tmp = new Triple(startAndEndNodes.getStartNode(), conferenceEndNode.get(i), relation.get(i));
            triple.add(tmp);
        }
        return triple;
    }

    @Override
    public List<PackageLabel> getAllLabels() {
        List<PackageLabel> packageLabels = new ArrayList<>();
        List<String> allLabels = labelsRepository.getAllLabels();
        for(String label : allLabels){
            PackageLabel packageLabel = new PackageLabel();
            packageLabel.setLabel(label);
            if(label.equals("人物")) {
                packageLabel.setRequestUrl(GetSubGraphUrl.PersonGraphUrl);
            } else if (label.equals("机构")) {
                packageLabel.setRequestUrl(GetSubGraphUrl.InstituteGraphUrl);
            }else if(label.equals("会议")){
                packageLabel.setRequestUrl(GetSubGraphUrl.ConferenceGraphUrl);
            }
            else if(label.equals("政策")){
                packageLabel.setRequestUrl(GetSubGraphUrl.PolicyGraphUrl);
            }
            else if(label.equals("文件")){
                packageLabel.setRequestUrl(GetSubGraphUrl.DocumentGraphUrl);
            }
            else if(label.equals("措施")){
                packageLabel.setRequestUrl(GetSubGraphUrl.MeasurementGraphUrl);
            }
            else if(label.equals("地区")){
                packageLabel.setRequestUrl(GetSubGraphUrl.RegionGraphUrl);
            }
            packageLabels.add(packageLabel);
        }
        return packageLabels;
    }

    @Override
    public List<Triple> getConferenceGraph() {
        List<Triple> conferences = new ArrayList<>();
        List<String> allConferenceNames = conferenceRepository.getAllConference();
        for(String conferenceName: allConferenceNames){
            StartAndEndNodes interConference = conferenceRepository.getConferenceGraph(conferenceName);
            List<Triple> triple = getTriple(interConference);
            conferences.addAll(triple);
        }
        return conferences;
    }

    @Override
    public List<Triple> getPersonGraph() {
        List<Triple> persons = new ArrayList<>();
        List<String> allPersonNames = personRepository.getAllPerson();
        for(String personName: allPersonNames){
            StartAndEndNodes interPerson = personRepository.getPersonGraph(personName);
            List<Triple> triple = getTriple(interPerson);
            persons.addAll(triple);
        }
        return persons;
    }

    @Override
    public List<Triple> getInstituteGraph() {
        List<Triple> institutes = new ArrayList<>();
        List<String> allInstituteNames = instituteRepository.getAllInstitute();
        for(String instituteName: allInstituteNames){
            StartAndEndNodes interInstitute = instituteRepository.getInstituteGraph(instituteName);
            List<Triple> triple = getTriple(interInstitute);
            institutes.addAll(triple);
        }
        return institutes;
    }

    @Override
    public List<Triple> getPolicyGraph() {
        List<Triple> policies = new ArrayList<>();
        List<String> allPoliciesNames = policyRepository.getAllPolicy();
        for(String policyName: allPoliciesNames){
            StartAndEndNodes interPolicy = policyRepository.getPolicyGraph(policyName);
            List<Triple> triple = getTriple(interPolicy);
            policies.addAll(triple);
        }
        return policies;
    }

    @Override
    public List<Triple> getDocumentGraph() {
        List<Triple> documents = new ArrayList<>();
        List<String> allDocumentsNames = documentRepository.getAllDocument();
        for(String documentName: allDocumentsNames){
            StartAndEndNodes interDocument = documentRepository.getDocumentGraph(documentName);
            List<Triple> triple = getTriple(interDocument);
            documents.addAll(triple);
        }
        return documents;
    }

    @Override
    public List<Triple> getMeasurementGraph() {
        List<Triple> measurements = new ArrayList<>();
        List<String> allMeasurementsNames = measurementRepository.getAllMeasurement();
        for(String measurementName: allMeasurementsNames){
            StartAndEndNodes interMeasurement = measurementRepository.getMeasurementGraph(measurementName);
            List<Triple> triple = getTriple(interMeasurement);
            measurements.addAll(triple);
        }
        return measurements;
    }

    @Override
    public List<Triple> getRegionGraph() {
        List<Triple> regions = new ArrayList<>();
        List<String> allRegionsNames = regionRepository.getAllRegion();
        for(String regionName: allRegionsNames){
            StartAndEndNodes interRegion = regionRepository.getRegionGraph(regionName);
            List<Triple> triple = getTriple(interRegion);
            regions.addAll(triple);
        }
        return regions;
    }

}
